// pages/myCoupons/myCoupons.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userCouponsId:[],//用户拥有的优惠券的id列表
    couponsList:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      navH: app.globalData.navHeight
    });
    var that = this;
    this.getAllCoupons();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 获取所有可用的优惠券
   */
  getAllCoupons: function () {
    var that = this;
    wx.request({
      url: app.globalData.url +'getValidCouponListAll',
      method: 'GET',
      data:{
        userId :wx.getStorageSync("userId"),
        category:0
      },
      success: function(res){
        that.setData({
          couponsList: res.data.data
        });
      },
    });
  },
  led:function(e){
    var that = this;
    wx.request({
      url: app.globalData.url + 'userReceiveCoupon',
      method: 'GET',
      data: {
        userId:wx.getStorageSync("userId"),
        couponId:e.target.dataset.couponid,
        category: 0
      },
      success: function (res) {
        wx.request({
          url: app.globalData.url + 'getValidCouponListAll',
          method: 'GET',
          data: {
            userId:wx.getStorageSync("userId")
          },
          success: function (res) {
            that.setData({
              couponsList: res.data.data
            });
          },
        });
      },
    });
  },
  navBack: function () {
    wx.navigateBack({
    })
  }
})